package com.zxd.service.impl;

import com.zxd.core.Constant;
import com.zxd.dao.FileDao;
import com.zxd.dao.GroupDao;
import com.zxd.entity.File;
import com.zxd.entity.Group;
import com.zxd.entity.vo.BaseVo;
import com.zxd.entity.vo.TreeVo;
import com.zxd.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * GroupServiceImpl
 * Created by 涂奕恒 on 2019/5/6 0006 下午 12:00.
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private FileDao fileDao;

    @Override
    public List<Group> findAll() {
        return groupDao.findAll();
    }

    @Override
    public Page<Group> find(int pageNum, int pageSize) {
        return groupDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Group> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Group getById(int id) {
        return groupDao.findOne(id);
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        for (File file : fileDao.findListByGroupId(id)) {
            fileDao.delete(file.getId());
        }

        groupDao.delete(id);
    }

    @Override
    public Group create(Group group) {
        return groupDao.save(group);
    }

    @Override
    public Group update(Group group) {
        return groupDao.save(group);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<TreeVo> groupList() {
        return getChildren(0, 1);
    }

    @Override
    public Integer saveGroup(Integer id, String name, Integer parentId) {
        Group group;
        Group groupByName = groupDao.paramsByNameAndParentId(name, parentId);

        if (null == id) {
            if (null != groupByName) {
                return -1;
            }
            group = new Group();
            group.setParentId(null == parentId ? 0 : parentId);

            if (null != parentId && parentId > 0) {
                group.setLevel(getById(parentId).getLevel() + 1);
            }
        } else {
            group = groupDao.findOne(id);
            if (null != groupByName && !group.getId().equals(groupByName.getId())) {
                return -1;
            }
            group.setUpdateTime(System.currentTimeMillis());
        }

        group.setName(name);

        groupDao.save(group);

        return 1;
    }

    @Override
    public BaseVo getGroup(Integer groupId) {
        BaseVo baseVo = new BaseVo();

        baseVo.setMsg(getById(groupId).getName());

        return baseVo;
    }

    private List<TreeVo> getChildren(Integer groupId, Integer level) {
        List<TreeVo> list = new ArrayList<>();
        TreeVo vo;

        for (Group group : groupDao.findListByParentId(groupId)) {
            vo = new TreeVo();
            vo.setId(group.getId());
            vo.setName(group.getName());
            vo.setLevel(group.getLevel());
            if (level <= 3) {
                vo.setChildren(getChildren(group.getId(), group.getLevel()));
            }

            list.add(vo);
        }

        return list;
    }
}